{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding: utf-8\n",
    "# import tensorflow as tf\n",
    "import datetime,time\n",
    "import pandas as pd\n",
    "import json\n",
    "from pymongo import MongoClient\n",
    "import fushare as ak\n",
    "from pandas import DataFrame\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "def get_trade_rank(market = 'SHF', date = None):\n",
    "    if date is None:\n",
    "        date = get_target_date(-1, \"%Y-%m-%d\")\n",
    "    if market == 'SHF':\n",
    "        return ak.get_shfe_rank_table(date)\n",
    "    if market == 'DCE':\n",
    "        return ak.get_dce_rank_table(date)\n",
    "    if market == 'CZC':\n",
    "        return ak.get_czce_rank_table(date)\n",
    "    if market == \"CFE\":\n",
    "        return ak.get_cffex_rank_table(date)\n",
    "    return None, '不支持的市场类型'\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    markets = ['CZC', 'SHF','CFE','DCE']#, 'CZC', 'SHF','CFE','DCE'\n",
    "    # 连接数据库\n",
    "    client = MongoClient('localhost', 27017)\n",
    "    db = client.futures3\n",
    "    position = db.position\n",
    "    \n",
    "#     df=pd.DataFrame()\n",
    "    for market in markets:\n",
    "        begin = datetime.date(2020,10,19)\n",
    "        # end = datetime.date(2020,2,25)\n",
    "        end = datetime.date.today()\n",
    "\n",
    "        # begin = DataFrame(list(position.find({}).sort([('_id', -1)]).limit(1)))\n",
    "        # begin = begin['date'][0]\n",
    "        # begin = time.strptime(begin, \"%Y%m%d\")\n",
    "        # year, month, day = begin[:3]\n",
    "        # begin = datetime.date(year, month, day)\n",
    "        # begin = begin + datetime.timedelta(days=1)\n",
    "\n",
    "        for i in range((end - begin).days + 1):\n",
    "            day = begin + datetime.timedelta(days=i)\n",
    "            days=day.strftime('%Y%m%d')\n",
    "            try:\n",
    "                df = get_trade_rank(market, date=days)\n",
    "                # print(days, market)\n",
    "                for key, value in df.items():\n",
    "                    value['date'] = days\n",
    "                    print(value)\n",
    "                    value['symbol']=value['symbol'].str.upper()\n",
    "                    value= value[value['long_party_name'].notna()]\n",
    "                    # print(value)\n",
    "                    # position.insert_many(json.loads(value.T.to_json()).values())\n",
    "                    # print(json.loads(value.T.to_json()).values())\n",
    "                    #去除具体合约。因汇总持仓有问题\n",
    "                    if market != 'CZC':\n",
    "                        print('insert into',key)\n",
    "                        # position.update_one(value, {'$set': value}, True)\n",
    "                        position.insert_many(json.loads(value.T.to_json()).values())\n",
    "\n",
    "                    else:\n",
    "                        value=value[value['symbol']==value['variety']]\n",
    "                        print('insert into',key)\n",
    "#                         df=df.append(value)\n",
    "                        # position.update_one(value, {'$set': value}, True)\n",
    "                        # position.insert_many(json.loads(value.T.to_json()).values())\n",
    "#                         print(json.loads(value.T.to_json()).values())\n",
    "            except:\n",
    "                print(days,market,'数据异常')\n",
    "                continue\n",
    "value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
